package com.quectel.util.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.quectel.util.ex.common.JsonConvertException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * jackson帮助类
 *
 * @author caolu
 */
public class JacksonUtils {
    /**
     * 通用的实例
     */
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
            .registerModules(
                    new SimpleModule()
                            .addSerializer(long.class, ToStringSerializer.instance)
                            .addSerializer(Long.class, ToStringSerializer.instance)
            );


    private JacksonUtils() {

    }

    /**
     * 转为string
     *
     * @param obj
     * @return
     */
    public static String toJsonString(Object obj) {
        try {
            return OBJECT_MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new JsonConvertException(e);
        }
    }

    /**
     * parse对象
     *
     * @param json
     * @return
     */
    public static JsonNode parse(String json) {
        try {
            return OBJECT_MAPPER.readTree(json);
        } catch (IOException e) {
            throw new JsonConvertException(e);
        }
    }

    /**
     * parse对象
     *
     * @param json
     * @param valueTypeRef new TypeReference<List<User>>(){}
     * @param <T>
     * @return
     */
    public static <T> T parse(String json, TypeReference<T> valueTypeRef) {
        try {
            return OBJECT_MAPPER.readValue(json, valueTypeRef);
        } catch (IOException e) {
            throw new JsonConvertException(e);
        }
    }

    /**
     * parse对象
     *
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T parse(String json, Class<T> clazz) {
        try {
            return OBJECT_MAPPER.readValue(json, clazz);
        } catch (IOException e) {
            throw new JsonConvertException(e);
        }
    }

    /**
     * parse一个数组
     *
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> parseArray(String json, Class<T> clazz) {

        try {
            return OBJECT_MAPPER.readValue(json, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
        } catch (IOException e) {
            throw new JsonConvertException(e);
        }
    }

    /**
     * parse一个数组
     *
     * @param json
     * @return
     */
    public static List<JsonNode> parseArray(String json) {
        return parseArray(json, JsonNode.class);
    }


}
